package com.hfzy.ihk.web.callhistoryold.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.web.callhistoryold.biz.CallHistoryOldBiz;
import com.hfzy.ihk.web.callhistoryold.entity.CallHistoryOld;
import com.hfzy.ihk.web.callhistoryold.vo.CallHistoryFzjConditionVo;
import com.hfzy.ihk.web.callhistoryold.vo.CallHistoryFzjListVo;
import com.hfzy.ihk.web.callhistoryold.vo.CallHistoryOldVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by Administrator on 2017/8/31.
 *
 * @author lht
 */
@Slf4j
@Controller
@RequestMapping("/callHis")
public class CallHistoryController {

    @Autowired
    CallHistoryOldBiz callHistoryOldBiz;

    /**
     * 房专家获取云呼记录列表
     * @param callHistoryFzjConditionVo
     * @return
     */
    @RequestMapping("/get_call_history_new.do")
    @ResponseBody
    public Map getFzjCallHistory(CallHistoryFzjConditionVo callHistoryFzjConditionVo) {
        log.info("调用:房专家get_call_history_new.do,参数:" + JSONObject.toJSONString(callHistoryFzjConditionVo));
        long start = System.currentTimeMillis();
        Map map = new HashMap();
        List<CallHistoryFzjListVo> list = callHistoryOldBiz.getCallhistoryListByCallHistoryFzjConditionVo(callHistoryFzjConditionVo);
        map.put("data",list);
        log.info("调用结束:房专家get_call_history_new.do,耗时:" + (System.currentTimeMillis() - start));
        return map;
    }


    @RequestMapping("upsert.do")
    @ResponseBody
    public Map<String, String> upsertCallHistory(String data){

        log.info("调用云呼推数据过来接口:upsert.do，参数:" + data);
        long start = System.currentTimeMillis();

        Map<String,String> map = new HashMap<>();
        if(StringTools.isEmpty(data)){
            map.put("code","-1");
            map.put("desc","参数为空");
            return map;
        }

        CallHistoryOldVo callHistoryVo = JSON.parseObject(data, CallHistoryOldVo.class);
        if(StringTools.isNotempty(callHistoryVo.getCreateUserId()) || StringTools.isNotempty(callHistoryVo.getPageFrom())){
            callHistoryVo.setIsPushKafka(true);
        }
        try {
            List<CallHistoryOldVo> callVoList = new ArrayList<>();
            callVoList.add(callHistoryVo);
            callHistoryOldBiz.saveDatas(callVoList);
            map.put("code","1");
            map.put("desc","成功");
            log.info("完成:调用云呼推数据过来接口,耗时:" + (System.currentTimeMillis() - start));
        } catch (Exception e) {
            log.info("执行upsertCallHistory,报错!",e);
            e.printStackTrace();
            map.put("code","-2");
            map.put("desc","更新异常");
        }


        return map;
    }


}
